const wx2my = require('../../wx2my');

var interval = null; //倒计时函数

const app = getApp();
Page({
  data: {
    img: ''
  },
  onLoad: function (e) {
    let username = app.fm.gain('username');
    let password = app.fm.gain('password');
    this.setData({
      username: username,
      password: password
    });
  },
  onReady: function () {
    this.get_img();
  },
  onShow: function () {},

  get_img() {
    let that = this;
    app.wxRequest('rotation_chart', {
      'type': 3
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let img = res.data.data[0];
        that.setData({
          img: img
        });
      }
    });
  },

  username(e) {
    let that = this;
    that.setData({
      username: e.detail.value
    });
  },

  password(e) {
    let that = this;
    that.setData({
      password: e.detail.value
    });
  },

  login() {
    let that = this;

    if (app.fm.isNull(this.data.username)) {
      app.toast('请输入商家账号');
    } else if (app.fm.isNull(this.data.password)) {
      app.toast('请输入密码');
    } else {
      app.wxRequest('shop_login', {
        'username': that.data.username,
        'password': that.data.password
      }, function (res) {
        console.log(res);

        if (res.data.code == 1) {
          app.fm.save('username', that.data.username);
          app.fm.save('password', that.data.password);
          app.fm.save('shop_token', res.data.data.shop_token);
          app.fm.save('type', res.data.data.type);

          if (res.data.data.type == 1) {
            //代理商
            wx2my.navigateTo({
              url: '/pages/agent_index/agent_index?type=' + 1
            });
          } else if (res.data.data.type == 2) {
            //场地商
            wx2my.navigateTo({
              url: '/pages/agent_index/agent_index?type=' + 2
            });
          }
        }

        app.toast(res.data.msg);
      });
    }
  },

  bindAgreeChange(e) {
    let that = this;
    console.log(e.detail.value);
    that.setData({
      agree: e.detail.value
    });
  },

  service_agreement() {
    wx2my.navigateTo({
      url: '/pages/service_contract/service_contract'
    });
  },

  forget() {
    wx2my.navigateTo({
      url: '/pages/password/password'
    });
  }

});